#include "protocolTable.h"
#include "version.h"
#include "logger.h"
#include "db.h"
#include <string.h>
#include <stdlib.h>

/**
 *   int64_t  id;          //协议ID
    char name[32];        //协议名称
    T_PROTOCOL_TYPE  type;//协议类型,一个产品最多支持8种协议类型
    char desc[256];       //协议
 */
#define PROTOCOL_TABLE_NAME "t_protocol"
#define SQL_PROTOCOL_TABLE_CREATE "CREATE TABLE IF NOT EXISTS " PROTOCOL_TABLE_NAME " (id INT PRIMARY KEY NOT NULL, name TEXT NOT NULL, type INT NOT NULL, desc CHAR(256))"
#define SQL_PROTOCOL_TABLE_INSERT(...) "INSERT INTO " PROTOCOL_TABLE_NAME "(id, name, type, desc) VALUES (" #__VA_ARGS__ ")"
#define SQL_PROTOCOL_TABLE_REPLACE(...) "REPLACE INTO " PROTOCOL_TABLE_NAME "(id, name, type, desc) VALUES (" #__VA_ARGS__ ")"
#define SQL_PROTOCOL_TABLE_DELETE(id) "DELETE FROM " PROTOCOL_TABLE_NAME " WHERE id = " #id
#define SQL_PROTOCOL_TABLE_SELECT(id) "SELECT * FROM " PROTOCOL_TABLE_NAME " WHERE id = " #id



/*===============================================================*/
/* 协议 */
int createProtocolTable(){
    return db_create_table(SQL_PROTOCOL_TABLE_CREATE);
}

int insertProtocol(T_PROTOCOL* protocol){
    if(NULL == protocol){
        log_file_e(APPNAME, "insert protocol is NULL");
        return -1;
    }
    if((NULL == protocol->name) || (1 > strlen(protocol->name))){
        log_file_e(APPNAME, "insert protocol name is NULL or empty");
        return -1;
    }
    return db_insert(SQL_PROTOCOL_TABLE_INSERT(protocol->id, protocol->name, protocol->type, protocol->desc));
}

int replaceProtocol(T_PROTOCOL* protocol){
    if(NULL == protocol){
        log_file_e(APPNAME, "replace protocol is NULL");
        return -1;
    }
    if((NULL == protocol->name) || (1 > strlen(protocol->name))){
        log_file_e(APPNAME, "replace protocol name is NULL or empty");
        return -1;
    }
    return db_insert(SQL_PROTOCOL_TABLE_REPLACE(protocol->id, protocol->name, protocol->type, protocol->desc));
}

int deleteProtocol(T_PROTOCOL* protocol){
    if(NULL == protocol){
        log_file_e(APPNAME, "delete protocol is NULL");
        return -1;
    }
    return db_delete(SQL_PROTOCOL_TABLE_DELETE(protocol->id));
}
/**
 *  
 */
static int protocolCallback(void *data, int argc, char **argv, char **azColName){
    int i = 0;
    if(NULL == data){
        log_file_e(APPNAME, "modelCallback data is NULL");
        return -1;
    }
    T_PROTOCOL_LIST* protocolList = (T_PROTOCOL_LIST*)data;
    if((0 == protocolList->count) || (NULL == protocolList->head)){
        protocolList->head = protocolList->tail = (T_PROTOCOL*)malloc(sizeof(T_PROTOCOL));
        protocolList->head->next = NULL;
        protocolList->head->prev = NULL;
    } else {
        protocolList->tail->next = (T_PROTOCOL*)malloc(sizeof(T_PROTOCOL));
        protocolList->tail->next->prev = protocolList->tail;
        protocolList->tail = protocolList->tail->next;
    }
    protocolList->count++;
    for(i = 0; i < argc; i++){
        if(0 == strcmp(azColName[i], "id")){
            protocolList->tail->id = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "name")){
            if(NULL == argv[i]){
                protocolList->tail->name[0] = '\0';
            } else {
                strcpy(protocolList->tail->name, argv[i]);
            }
        } else if(0 == strcmp(azColName[i], "type")){
            protocolList->tail->type = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "desc")){
            if(NULL == argv[i]){
                protocolList->tail->desc[0] = '\0';
            } else {
                strcpy(protocolList->tail->desc, argv[i]);
            }
        }
        log_i("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    return protocolList->count;
}

int selectProtocol(const char* sql, T_PROTOCOL_LIST* protocolList){
    if((NULL == sql) || (1 > strlen(sql)) || (NULL == protocolList)){
        log_file_e(APPNAME, "select protocol sql is NULL");
        return -1;
    }
    return db_select(sql, protocolCallback, protocolList);

}
//
void dbProtocolInit(){
    createProtocolTable();
}
